#!/usr/bin/perl
use warnings;
use strict;

my %GroupId_Content;
my $workdir = "/home/pingtest";
my $Countf = "$workdir/count.txt";
my $Ping_Count;

my $url = 'http://jx3.client.cdn.kingsoft.com/JXOnline3-v1.6.6.2291.jx3.xoyo.com.rar';
my $yuming = (split(/\//,$url))[2];

sub getdatetime{
	my ($sec, $min, $hour, $day, $mon, $year, $wday, $yday, $isdst) = localtime(time());
	$year = 1900 + $year;
	$mon = $mon + 1;
	return (sprintf("%04d-%02d-%02d",$year,$mon,$day),sprintf("%02d:%02d:%02d",$hour,$min,$sec));
}

sub usage{
	print <<EOF;
	Usage: $0 ipconfig_file
	If you don't provide any parameters, ipconfig_file is the default value of "$workdir/ip.conf". File contents like this:
	#GroupID IPaddress Packet_Send
	1 1.1.1.1 10
	1 1.1.1.2 10
	s 1.1.1.3 10
	s 2.2.2.1 10
	2 2.2.2.2 10

	last-modified : 2008-7-11 1:17:49
EOF
	my @dt = getdatetime();
	print "@dt\n";
	exit(0);
}

sub init{
	my $ipfig = "$workdir/ip.conf";
	if(@ARGV) {
		$ipfig = shift @ARGV;
	}
	unless(-e $ipfig or -f _ or -r _){
		print "ipconfig_file[$ipfig] isn't exists!\n\n";
		usage();
	}
	open IPFIG,$ipfig or die "Error in sub init: $ipfig,$!\n";
	while(<IPFIG>) {
		chomp $_;
		s/^\s+//;
		s/\s+$//;

		next if m/^#/ or m/^;/;
		my @c = split(/\s+/,$_);
		if(@c < 3){
			print "[$_] is Error line, in [$ipfig]\n";
			next;
		}
		$GroupId_Content{$c[0]} .= "$c[1]:$c[2] ";
	}
	close IPFIG;
	unless(-e $Countf) {
		open COUNTF,">$Countf" or die "Error in sub init: $Countf,$!\n";
		print COUNTF "1";
		close COUNTF;
		$Ping_Count = 1;
	}else{
		open COUNTF,$Countf or die "Error in sub init: $Countf,$!\n";
		$Ping_Count = (<COUNTF>)[0];
		chomp $Ping_Count;
		close COUNTF;
	}
}

sub analog {
	my @result = @_;
	my ($max,$avg,$min,$loss);
	foreach my $line ( @result ) {
		chomp $line;
		if($line =~ /,/||$line =~ /rtt/){
			foreach my $s (split(/,/,$line)) {
				$s =~ s/^\s+//;
				$s =~ s/\s+$//;
				if($s =~ /packet loss/) {
					$loss = (split(/\s+/,$s))[0];
					if ( $loss eq "100%") {
						($max,$avg,$min) = qw|0 0 0|;
						last;
					}
					next;
				}
				if($s =~ /^rtt/) {
					my $d = (split(/=/,$s))[-1];
					$d =~ s/^\s+//;
					$d =~ s/\s+$//;
					$d =~ s/\// /g;
					($max,$avg,$min) = (split(/\s+/,$d));
					next;
				}
			}
		}
	}
	return "$max\t$avg\t$min\t$loss";
}

sub storlog{
	my $date = (getdatetime())[0];
	my $LogFile="$workdir/$date.log";

	unless(-e $LogFile) {
		open LOGF , ">$LogFile" or die "Error in sub storlog:$LogFile,$!\n";
		print LOGF "#PingID\tIP_Group_ID\tIPaddress\tTime\tMaxValue\tAvgValue\tMinValue\tPacket_Loss\tspeed_download(K)\n";
		close LOGF;

		open COUNTF,">$Countf" or die "Error in sub init: $Countf,$!\n";
		print COUNTF "1";
		close COUNTF;
		$Ping_Count = 1;
	}
	open LOGF , ">>$LogFile" or die "Error in sub storlog:$LogFile,$!\n";
	print LOGF "$_[0]\t$_[1]\t$_[2]\t$_[3]\t$_[4]\t$_[5]\n";
	close LOGF;
}

sub ping {
	my ($gid,$cont) = @_;
	
	foreach ( split(/\s+/,$cont) ) {
		my @ip_ps = split(/:/,$_);
		my $cmd = "ping -i 0.01 $ip_ps[0] -c $ip_ps[1]";
               `sed -i "/$yuming/d" /etc/hosts`;
               `echo "$ip_ps[0] $yuming">>/etc/hosts`;
		my $result2=`/usr/bin/curl -o /dev/null -r 0-10000000 --trace-ascii /tmp/wget-check.tmp -H "Pragma:" -s  -B --connect-timeout 15 -w %{http_code}:%{time_namelookup}:%{time_connect}:%{time_pretransfer}:%{time_starttransfer}:%{time_total}:%{size_download}:%{speed_download} $url`;
		my($http_code,$timp_namelookup,$time_connect,$pretransfer,$time_firstbyte,$time_total,$size,$speed)=split(/\:/,$result2);
		$speed=int($speed/1024);
print "$speed\t";
		my $time = (getdatetime())[-1];
		my @result = `$cmd`;
		my $anastr = analog(@result);

		storlog($Ping_Count,$gid,$ip_ps[0],$time,$anastr,$speed);
	}
}

sub star {
	foreach my $gid ( keys %GroupId_Content ) {
		my $cont = $GroupId_Content{$gid};
		$cont =~ s/^\s+//;
		$cont =~ s/\s+$//;
		ping($gid,$cont);
	}
}

sub main{
	init();
	star();
	$Ping_Count++;
	open COUNTF,">$Countf" or die "Error in sub main: $Countf,$!\n";
	print COUNTF $Ping_Count;
	close COUNTF;
}

main();

